import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import * as echarts from "echarts";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import axios from "axios";
// vue中导出excel表格模板
import FileSaver from "file-saver";
import XLSX from "xlsx";
//vue导出excel表格
import JsonExcel from "vue-json-excel";

//图表
Vue.prototype.$echarts = echarts;
Vue.component("downloadExcel", JsonExcel);
// 将excel表格模板设置全局
Vue.prototype.$FileSaver = FileSaver;
Vue.prototype.$XLSX = XLSX;

axios.interceptors.request.use(
  (config) => {
    // 把jwt拼到每个url的请求头中
    // 先获取jwt
    let jwt = localStorage.getItem("token");
    // 如果jwt非空，拼接到请求头
    if (jwt) {
      config.headers.token = jwt;
    }
    return config;
  }

);
axios.interceptors.response.use(
  (response) => {
    // console.log("response", response);
    return response;
  },
  (err) => {
    if (err.response.status == 403) {
      alert("未有该权限");
      return;
    }
  }
);
//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {
  //如果路由需要跳转
  if (to.name != "login") {
    axios
      .get("api/uums-server/token")
      .then((res) => {
        next();
      })
      .catch((err) => {
        next({ path: "/login" });
      });
  } else {
    next();
  }
});

Vue.config.productionTip = false;

Vue.prototype.$axios = axios;
Vue.use(ElementUI);

new Vue({
  router,
  render: (h) => h(App),
}).$mount("#app");
